home *** CD-ROM | disk | FTP | other *** search
- Path: winternet.com!mschwarz
- From: mschwarz@winternet.com (Michael Schwarz)
- Newsgroups: comp.lang.c
- Subject: Watch out! C "gotcha!"
- Date: 14 Feb 1996 20:43:29 GMT
- Organization: StarNet Communications, Inc
- Message-ID: <4fthhh$7th@blackice.winternet.com>
- NNTP-Posting-Host: subzero.winternet.com
- X-Newsreader: TIN [version 1.2 PL2]
-
- Many of you may already know this, but even after ten years of coding
- in C, this took my by surprise at first. A co-worker and I were looking
- at some library C code where I work and we noticed that the "default"
- clause of one of the switches was misspelled as "defalt."
-
- He said, "This must have been changed since it was last compiled."
-
- "What the heck, we're in development. Run make."
-
- (We're on an RS-6000 here, BTW). Make ran and silently compiled the
- module. No warning. No error. We fired the editor back up and I
- changed it to "token" and compiled again. No warning, no error.
-
- I came to the (in retrospect foolish) conclusion that the compiler must
- take any != 'case' string as the default. I asked him to try it under
- Microsoft Visual C/C++ 1.0 while I went and tried it with Borland 3.1
-
- Borlaqnd compiled without warning or error. While I was at it, I tried
- running the following simple program:
-
- #include <stdio.h>
-
- main()
- {
- int x = 2;
-
- switch (x)
- {
- case 1:
- printf("It's 1.\n");
- break;
-
- defalt:
- printf("It's not 1.\n");
- break;
- }
-
- return 0;
- }
-
-
- No output. Now this was a little more serious. Then it hit me. How about
- this version:
-
- int x = 1;
-
- switch (x)
- {
- case 1:
- printf("It's 1.\n");
- goto defalt;
- break;
-
- Sure enough. The output of the program now is:
-
- It's 1.
- It's not 1.
-
- My co-worker came back and said it compiled but there was an unreferenced
- label warning. Well, of course there was!
-
- My point is that several compilers we tried this on compiled this mistake
- with no error or warning. If you, like so many of us, do not get enough
- time to completely coverage test your code, this mistake can really bite
- you on the behind. Your "default" code will never be executed!
-
- Does anyone know of:
-
- 1) A specific remedy for the "cc/xlc" compilers under AIX, perhaps
- an option that turns on a warning similar to the one we got from
- MSVC?
-
- 2) A general workaround for this, e.g., something that will always
- catch this.
-
- 3) The ANSI party line on labels -- is there an ANSI enforcement option
- that guarantees a warning on a construct like this? (If not, does
- anyone know how to suggest revisions to the X3-J11 standard?)
-
- The thing that scares me the most about this is that either I've never before
- made this particular mistake or for the last ten years I've been leaving
- behind a number of ticking code bombs...
-
- I invite your comments on this (and your knowledge -- I'm a good C programmer
- but I don't write compilers and I'm not fully conversant with the full
- ANSI spec on C). Please feel free to e-mail me.
-
- --
- Michael A. Schwarz Minneapolis, MN
- mschwarz@winternet.com n0zes@n0zes.ampr.org
-
-